package com.example.user.controller;


import com.example.user.common.Result;
import com.example.user.entity.User;
import com.example.user.entity.UserLogin;
import com.example.user.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin(origins = "http://localhost:8080")
@RequestMapping("/user")
public class UserController {


    @Autowired
    private UserService userService;


    /**
     * 用户列表展示
     * @param pageNum
     * @param pageSize
     * @return
     */

    @GetMapping("/getUserPage")
   public Result<PageInfo<User>>  getUserPAge(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize){
        PageHelper.startPage(pageNum, pageSize);
        List<User> userList = userService.getUserPage();
        PageInfo<User> pageInfo = new PageInfo<>(userList);
        return Result.success(pageInfo);
    }

    /**
     * 新增用户信息
     */
    @PostMapping("/addUser")
    public Result addUSer(@RequestBody User user){
        Result result = userService.addUser(user);
        return result;
    }

    /**
     * 根据id进行删除用户操作
     *
     */
    @DeleteMapping("/deleteUserById/{id}")
    public Result deleteUserById(@PathVariable Integer id){
        Result result = userService.deleteUserById(id);
        return result;
    }

    /**
     * 根据用户名进行搜索
     */
    @GetMapping("/getUseByName")
    public Result<PageInfo<User>> getUserByName(@RequestParam String username){
        PageHelper.startPage(1, 10);
        List<User> users = userService.getUserByName(username);
        PageInfo<User> pageInfo = new PageInfo<>(users);

        return Result.success(pageInfo);
    }

    /**
     * 根据id编辑用户信息
     */

    @PutMapping("/editUserInfo")
    public Result editUserInfo(@RequestBody User user){
        Result result = userService.editUserInfo(user);
        return result;
    }


    /**
     * 登录接口
     */

    @PostMapping("/login")
    public Result login(@RequestBody UserLogin userLogin){
        Result result = userService.login(userLogin);
        return result;
    }




    /**
     * 注册接口
     */


    @PostMapping("/register")
    public Result register(@RequestBody UserLogin user){

       Result result= userService.register(user);
       return result;
    }


    @GetMapping("/selectUserById")
    public Result selectUserById(@RequestParam Integer id){
       Result result= userService.selectUserById(id);
       return result;
    }




}
